﻿var map, currentPositionMarker, timer;
var markers = [];
var marker;
var marker2;

function CerrarSesion() {
    localStorage.removeItem("usuAct");
    window.location.href = 'index.html';
}

function ListRutas() {
    var parametro = {};
    var DirSer = localStorage.getItem("DirSerUsu");
    var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/getRutas";
    $.ajax({
        type: "GET",
        url: ruta,
        data: parametro,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var lrutas = (typeof result.d) == 'string' ? eval('(' + result.d + ')') : result.d;
            var color = 0;
            $.each(lrutas, function (index, item) {
                if(item.NomRuta != 'SIN RUTA'){
                    $("#lRutas").append("<button id='" + item.NomRuta + "' type='button' class='btn btn-warning' data-dismiss='modal' onclick='AntesVerRuta(id)'>" + item.NomRuta + "</button>");
                    //DibujarRutaHome(item.NomRuta, color);
                    //color = color + 1;
                } 
            });
        },
        error: function (jqXHR, status, error) {
            //alert(error + "-" + jqXHR.responseText);
        }
    });
}

function checkUsuario() {
    var usuAct = localStorage.getItem("usuAct");
    if (usuAct == null) {
        window.location.href = "index.html";
    } else {
        ListRutas();
    }
}

function AntesVerRuta(nombreRuta) {
    var parRuta = escape(nombreRuta);
    var Pagina = "VisualizarRuta.html?parRuta=" + parRuta;
    window.location.href = Pagina;
}

function getGET() {
    var loc = document.location.href;
    var getString = loc.split('?')[1];
    var GET = getString.split('&');
    var get = {};//this object will be filled with the key-value pairs and returned.
    for (var i = 0, l = GET.length; i < l; i++) {
        var tmp = GET[i].split('=');
        get[tmp[0]] = unescape(decodeURI(tmp[1]));
    }
    return get;
}

// Add a marker to the map and push to the array.
function addMarker(location) {
    var marker = new google.maps.Marker({
        position: location,
        map: map
    });
    markers.push(marker);
}

// Sets the map on all markers in the array.
function setAllMap(map) {
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(map);
    }
}

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
    setAllMap(null);
}

// Shows any markers currently in the array.
function showMarkers() {
    setAllMap(map);
}

// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
    clearMarkers();
    markers = [];
}

function DibujarRutaHome(NomRutaHome,color) {
    var polyOptions = {
        strokeColor: SelectColorRutaHome(color),
        strokeOpacity: 1.0,
        strokeWeight: 1,
    };
    var poly = {};
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
    var parametro = {
        NomRuta: "'" + NomRutaHome + "'"
    };
    var DirSer = localStorage.getItem("DirSerUsu");
    var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/getCoordenadasRuta";
    $.ajax({
        type: "GET",
        url: ruta,
        data: parametro,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var lpuntos = (typeof result.d) == 'string' ? eval('(' + result.d + ')') : result.d;
            $.each(lpuntos, function (index, item) {
                var path = poly.getPath();
                path.push(new google.maps.LatLng(item.lat, item.lon));
            });
        },
        error: function (jqXHR, status, error) {
            alert(error + "-" + jqXHR.responseText);
        }
    });
}

function SelectColorRutaHome(Sele) {
    switch (Sele) {
        case 0:
            return "#000000";
            break;
        case 1:
            return "#0000FF";
            break;
        case 2:
            return "#A52A2A";
            break;
        case 3:
            return "#00008B";
            break;
        case 4:
            return "#DC143C";
            break;
        case 5:
            return "#006400";
            break;
        case 6:
            return "#8B0000";
            break;
        case 7:
            return "#483D8B";
            break;
        case 8:
            return "#9400D3";
            break;
        case 9:
            return "#B22222";
            break;
        case 10:
            return "#008000";
            break;
        case 11:
            return "#4B0082";
            break;
        case 12:
            return "#7CFC00";
            break;
        case 13:
            return "#20B2AA";
            break;
        case 14:
            return "#00FF00";
            break;
        case 15:
            return "#800000";
            break;
        case 16:
            return "#0000CD";
            break;
        case 17:
            return "#008080";
            break;
        case 18:
            return "#9ACD32";
            break;
        case 19:
            return "#FF6347";
            break;
        case 20:
            return "#A0522D";
            break;


    }

}